package com.hjwoo.app.views;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.hjwoo.app.R;
import com.hjwoo.app.baseviews.BaseAppCompatActivity;
import com.hjwoo.app.been.FailureResultObject;
import com.hjwoo.app.common.LoanApplication;
import com.hjwoo.app.net.RequestListener;
import com.hjwoo.app.net.RequestMethod;
import com.hjwoo.app.net.RequestTag;
import com.hjwoo.app.utils.JsonParser;
import com.hjwoo.app.utils.LoanAccessTokenIdKeeper;
import com.hjwoo.app.utils.LoanLog;
import com.hjwoo.app.utils.LoanToast;
import com.hjwoo.app.utils.Utils;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

/**
 * 小区信息选择所在城市
 */
public class CommunityActivity extends BaseAppCompatActivity {
    public static final int REQUEST_CODE = 1;
    private static final String TAG = CommunityActivity.class.getSimpleName();
    //获取Activity实例
    public static CommunityActivity instance;
    TextView community_city_tv;
    Button community_next_btn;
    String curCity;
    String[] strArray;
    List<String> cityList = new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_community);

        Utils.initTitle("小区信息", this, true);
        instance = CommunityActivity.this;
        community_city_tv = (TextView) findViewById(R.id.community_city_tv);
        community_next_btn = (Button) findViewById(R.id.community_next_btn);
        curCity = community_city_tv.getText().toString();

        RequestMethod.getMethod().appQueryCity(LoanAccessTokenIdKeeper.readAccessToken(LoanApplication.getInstance()).getAppSessionId(),
                new RequestListener() {
                    @Override
                    public void requestSuccess(String tag, String message) {
                        if (tag == RequestTag.APPQUERYCITY) {
                            LoanLog.e("ee", message);
                            try {
                                JSONArray myJsonArray = new JSONArray(message);
                                for (int i = 0; i < myJsonArray.length(); i++) {
                                    //获取每一个JsonObject对象
                                    String item = myJsonArray.getString(i);
                                    cityList.add(myJsonArray.getString(i));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                        strArray = cityList.toArray(new String[]{});
                    }

                    @Override
                    public void requestError(String message) {
                        FailureResultObject failureResultObject = new FailureResultObject();
                        failureResultObject = JsonParser.getJsonParser().parserJsonToObj(message, FailureResultObject.class);
                        LoanToast.showToastShort(failureResultObject.getErrorMessage(), CommunityActivity.this);

                    }
                }, CommunityActivity.this);

        community_city_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (strArray != null) {
                    Bundle b = new Bundle();
                    b.putSerializable("ls", strArray);
                    Intent i = new Intent(CommunityActivity.this, SelectListActivity.class);
                    i.putExtras(b);
                    i.putExtra("equalStr", curCity);
                    startActivityForResult(i, REQUEST_CODE);

                } else {
                    LoanToast.showToastShort("网络有点卡，请稍后重试", CommunityActivity.this);
                }
            }
        });
        community_next_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(CommunityActivity.this, CellInformationActivity.class);
                i.putExtra("city", curCity);
                startActivity(i);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //先判断是哪个页面返回过来的
        switch (resultCode) {
            case RESULT_OK:
                String cityName = data.getStringExtra("itemName");
                community_city_tv.setText(cityName);
                curCity = cityName;
                break;
        }
    }
}
